/*
 * @Author: New Name 1278138741@qq.com
 * @Date: 2024-12-19 15:28:49
 * @LastEditors: New Name 1278138741@qq.com
 * @LastEditTime: 2024-12-23 19:00:23
 * @FilePath: \stray animals\src\stores\animalStore.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import axios from "axios";
export const useAnimalStore = defineStore("animals", {
  state: () => ({
    dogs: [],
  }),
  getters: {
    getDogById: (state) => (id) => {
      return state.dogs.find((dog) => dog.id === parseInt(id));
    },
  },
  actions: {
    async fetchDogs(dwType, blogType) {
      try {
        const response = await axios.post(
          "http://localhost:8080/Petrescue/blog/Blogs",
          {dwType, blogType}
        );
        const { code, data } = response.data;
        if (code == 200) {
          this.dogs = data.blogs;
        } else {
        }
      } catch (error) {
        console.error("Failed to fetch dogs:", error);
      }
    },
    async seach(quer) {
      try {
        const response = await axios.post(
          "http://localhost:8080/Petrescue/blog/Blogs",
          {
            seach,
            quer,
          }
        );
        const { code, data } = response.data;
        if (code == 200) {
          this.dogs = data.blogs;
        } else {
        }
      } catch (error) {
        console.error("Failed to fetch dogs:", error);
      }
    },

    setDogs(dogs) {
      this.dogs = dogs;
    },
  },
});
